package cn.iocoder.yudao.module.student.controller.app.message;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.SortingField;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.student.dal.dataobject.student.WebStudentDO;
import cn.iocoder.yudao.module.student.service.student.WebStudentService;
import cn.iocoder.yudao.module.system.api.message.MessageApi;
import cn.iocoder.yudao.module.system.api.message.MessageTypeApi;
import cn.iocoder.yudao.module.system.api.message.dto.*;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Api(tags = "管理后台-学生端-站内信")
@RestController
@RequestMapping("/student/message")
@Validated
public class StudentMessageController {

    @Resource
    private MessageApi messageApi;
    @Resource
    private MessageTypeApi messageTypeApi;
    @Resource
    private WebStudentService webStudentService;
    @Resource
    private AdminUserApi adminUserApi;

    @PostMapping("/get")
    @ApiOperation("获得站内信留言")
    public CommonResult<MessageRespDTO> getMessage(@RequestBody MessagePageReqDTO messagePageReqDTO) {
        MessageUpadateReqDTO messageUpadateReqDTO=new MessageUpadateReqDTO();
        MessageRespDTO messageRespDTO= messageApi.getMessage(messagePageReqDTO.getId());
        MessageTypeRespDTO messageTypeRespDTO=messageTypeApi.getMessageType(messageRespDTO.getType());
        if(messageTypeRespDTO !=null){
            messageRespDTO.setTypeName(messageTypeRespDTO.getName());
        }else{
            messageRespDTO.setTypeName("类型已删除");
        }
        BeanUtils.copyProperties(messageRespDTO,messageUpadateReqDTO);
        if(messageRespDTO.getStatus().equals("2")){
            messageUpadateReqDTO.setStatus("3");
            messageApi.updateMessage(messageUpadateReqDTO);
        }
        if(messageRespDTO.getStatus().equals("5")){
            messageUpadateReqDTO.setStatus("6");
            messageApi.updateMessage(messageUpadateReqDTO);
        }
        return success(messageRespDTO);
    }

    @PostMapping("/page")
    @ApiOperation("获得站内信留言分页")
    public CommonResult<PageResult<MessageRespDTO>> getMessagePage(@RequestBody MessagePageReqDTO messagePageReqDTO) {
        SortingField sortingField = new SortingField();
        sortingField.setField("createTime");
        sortingField.setOrder("desc");
        List<SortingField> sortingFields =new ArrayList<>();
        sortingFields.add(sortingField);
        messagePageReqDTO.setSortField(sortingField);
        messagePageReqDTO.setSortingFields(sortingFields);
        PageResult<MessageRespDTO> messagePage=messageApi.getMessagePage(messagePageReqDTO);
        MessageTypePageReqDTO pageVO =new MessageTypePageReqDTO();
        pageVO.setPageNo(1);
        pageVO.setPageSize(100);
        PageResult<MessageTypeRespDTO> messageTypePage=messageTypeApi.getMessageTypePage(pageVO);
        List<MessageRespDTO> messageRespDTOList=messagePage.getList();
        List<MessageTypeRespDTO> messageTypeRespDTOList=messageTypePage.getList();
        Map<Long,String> messageTypeMap=new HashMap<>();
        for (MessageTypeRespDTO messageTypeRespDTO:messageTypeRespDTOList) {
            messageTypeMap.put(messageTypeRespDTO.getId(),messageTypeRespDTO.getName());
        }

        for (MessageRespDTO messageRespDTO:messageRespDTOList) {
            messageRespDTO.setTypeName(messageTypeMap.get(messageRespDTO.getType()));
        }
        messagePage.setList(messageRespDTOList);
        return success(messagePage);
    }

    @PostMapping("/sendMessage")
    @ApiOperation("发起站内信留言")
    public CommonResult<Boolean> sendMessage(@RequestBody MessageCreateReqDTO messageCreateReqDTO) {
        messageCreateReqDTO.setStatus("0");
        messageCreateReqDTO.setType(messageCreateReqDTO.getType());
        messageCreateReqDTO.setStudentId(SecurityFrameworkUtils.getLoginUserId());
        messageApi.sendMessage(messageCreateReqDTO);
        return success(true);
    }

    @PostMapping("/replyMessage")
    @ApiOperation("站内信留言回复")
    public CommonResult<Boolean> replyMessage(@RequestBody MessageInfoCreateReqDTO messageInfoCreateReqDTO) {
        MessageRespDTO messageRespDTO= messageApi.getMessage(messageInfoCreateReqDTO.getMessageId());
        if(StringUtils.isNotBlank(messageRespDTO.getStatus()) && "4".equals(messageRespDTO.getStatus())){
            return error("平台运营正在处理，请耐心等待～");
        }
        MessageUpadateReqDTO messageUpadateReqDTO=new MessageUpadateReqDTO();
        BeanUtils.copyProperties(messageRespDTO,messageUpadateReqDTO);
        messageUpadateReqDTO.setStatus("4");
        messageApi.updateMessage(messageUpadateReqDTO);
        WebStudentDO webStudentDO=webStudentService.getWebStudent(SecurityFrameworkUtils.getLoginUserId());
        messageInfoCreateReqDTO.setReply(webStudentDO.getNickname());
        messageInfoCreateReqDTO.setReplyType(1);
        messageApi.replyMessage(messageInfoCreateReqDTO);
        return success(true);
    }

    @PostMapping("/open")
    @ApiOperation("发起站内信留言")
    public CommonResult<Boolean> open(@RequestBody List<Long> ids) {
        for (Long id:ids) {
            MessageUpadateReqDTO messageUpadateReqDTO = new MessageUpadateReqDTO();
            messageUpadateReqDTO.setId(id);
            messageUpadateReqDTO.setStatus("3");
            messageApi.updateMessage(messageUpadateReqDTO);
        }
            return success(true);
    }
    @PostMapping("/noOpenMessageCount")
    @ApiOperation("未读取站内信数量")
    public CommonResult<Integer> noOpenMessageCount() {
        return success(messageApi.noOpenMessageCount(SecurityFrameworkUtils.getLoginUserId()));
    }


    @PostMapping("/typeList")
    @ApiOperation("发起站内信留言")
    public CommonResult<List<MessageTypeRespDTO>> getTypeList() {
        return success(messageTypeApi.getMessageTypeList());
    }

    @PostMapping("/changeMessageStatus")
    @ApiOperation("修改站内信已读")
    public CommonResult<Boolean> changeMessageStatus(@RequestBody MessageUpadateReqDTO messageUpadateReqDTO) {
        messageApi.changeMessageStatus(messageUpadateReqDTO.getIds(),messageUpadateReqDTO.getStatus(),1);
        return success(true);
    }

    @PostMapping("/allchangeMessageStatus")
    @ApiOperation("修改站内信全部已读")
    public CommonResult<Boolean> allchangeMessageStatus(@RequestBody MessageUpadateReqDTO messageUpadateReqDTO) {
        messageApi.allchangeMessageStatus(messageUpadateReqDTO.getStatus(),1, SecurityFrameworkUtils.getLoginUserId(),null);
        return success(true);
    }
}
